package org.dinqy.impl;

import org.dinqy.ClauseBeanFactory;
import org.dinqy.Criterion;
import org.dinqy.syntax.AndOr;

/**
 * @author armandino (at) gmail.com
 */
public class AndOrNode<T> extends AscDescNode<T> implements AndOr<T> {

    AndOrNode(ClauseNode<T> parent) {
        super(parent);
    }

    @Override
    public AndOr<T> and(String fieldName, Criterion criterion) {
        setClauseBean(ClauseBeanFactory.and(fieldName, criterion));
        return createSuccessor();
    }

    @Override
    public AndOr<T> or(String fieldName, Criterion criterion) {
        setClauseBean(ClauseBeanFactory.or(fieldName, criterion));
        return createSuccessor();
    }

    @Override
    public long count() {
        return getQueryExecutor().count(getEntityClass(), getClauseBeans());
    }
    
    private AndOr<T> createSuccessor() {
        AndOrNode<T> successor = new AndOrNode<T>(this);
        setSuccessor(successor);
        return successor;
    }

}
